import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '公司',		name: 'comp_id',	type: 'select', loading:true, nameStr:'company_name', idStr:'uuid' },
			{ label: '状态',		name: 'enabled',	type: 'select', loading:true },
			{ label: '部门名称',		name: 'keyword' },
		],
		// 提交表单列表
		formData : [
			{ label: '部门名称',		name: 'depart_name', 	half:true, required:true },
			{ label: '中文简称',		name: 'short_name', 	half:true },
			{ label: '英文名称',		name: 'en_name', 		half:true },
			{ label: '英文简称',		name: 'short_en_name', 	half:true },
			{ label: '部门负责人',	name: 'manager', 		half:true },
			{ label: '固定电话',		name: 'tel', 			half:true, required:true },
			{ label: '排序',		name: 'sort', 			half:true, required:true, value:1,	type: 'number', },
		]
	}
	
	model = {}
	
	componentDidMount(){
		// 获取上级页面参数
		const { uuid } =  $fn.query()
		if(uuid){ 
			this.query = { comp_id: uuid }
			this.uuid = uuid
		}
		//
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => { 
		const { forms } = this.state
		$cache.then( async f => {
			// 公司
			$fn.setRow(forms, 'comp_id', await f.getCache({ api:'company' } ))
			// 回显公司
			if(this.uuid){
				this.searchFormRef.setValue({ comp_id: this.uuid  })
			}
			// 状态
			const enabledData = $fn.setRow( forms, 'enabled', await f.company_state)
			//
			this.setState({ forms, enabledData })
		})                       
	}
	// paging
	fetch = () => $fn.fetch.call(this,'department/index')
	// 表单
		cols = [
			{ title: '部门编号',		dataIndex: 'depart_code', 		width:150 },
			{ title: '部门名称', 	dataIndex: 'depart_name', 		width:150 },
			{ title: '部门负责人', 	dataIndex: 'manager', 			...$fn.table.name },
			{ title: '简称', 		dataIndex: 'short_name', 		width:100 },
			{ title: '英文名称', 	dataIndex: 'en_name', 			width:100 },
			{ title: '英文简称', 	dataIndex: 'short_en_name', 	width:100 },
			{ title: '所属公司', 	dataIndex: 'company_name', 		width:150 },
			{ title: '状态', 		dataIndex: 'enabled', 			...$fn.table.status, render: text => $fn.getState(this.state.enabledData, text) },
			TableControls.controls.call(this, { width:215, align:'left'})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, enabled, pid }) => {
		return [
			TableControls.edit.call(this, 'department/info', { uuid }, { ids:{ pid }}),			// 编辑
			(+enabled === -1 || +enabled === 0) && this.onEnabled( 1,  [ uuid ] ),	// 启用
			(+enabled === 1  || +enabled === 0) && this.onEnabled( 0,  uuid ),		// 禁用
			TableControls.pushTo.call(this, '查看', `keshi-manage?uuid=${uuid}`,{
				disabled : +enabled === 1 ? false : true
			}),
		]
	}
	// 操作按钮
	controls = () =>  [
		Controls.add.call(this),			// 添加
		this.onEnabled(1),					// 启用
		this.uuid && Controls.back.call(this)
	]
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'department/open', off:'department/del', isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='部门管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'department/add', editApi:'department/edit', param:{pid:''}, onRefresh:()=>{
					$fn.dataRemove('department/select')
				} }) }
			</>
		)
	}
}

export default Index